package com.xmqian.app.project.ui.user.presenter

import com.coszero.common.base.mvp.BaseModel
import com.coszero.common.base.mvp.BasePresenter
import com.coszero.common.utils.gson.GsonUtil
import com.xmqian.app.project.common.helper.RetrofitHelper
import com.xmqian.app.project.ui.user.contract.LoginContract
import com.xmqian.app.project.ui.user.model.LoginModel
import io.reactivex.functions.Consumer
import okhttp3.RequestBody

class LoginPresenter(mView: LoginContract.View?) :
    BasePresenter<LoginContract.View?>(mView), LoginContract.Presenter {
    override fun doLogin(username: String, pwd: String) {
        val map: MutableMap<String, String> = HashMap()
        map["username"] = username
        map["password"] = pwd
        val s = GsonUtil.toJsonStr(map)
        val requestBody = RequestBody.create(parse.parse("application/json"), s)
        addTask(
            RetrofitHelper.getInstance().service.login(requestBody)
        ) { resp: BaseModel<LoginModel> ->
            val data = resp.data
            view!!.onLoginSucceed(data)
        }
    }

    /**
     * 快捷登录
     *
     * @param username
     * @param smsCode
     */
    override fun quickLogin(username: String, smsCode: String) {
        val map: MutableMap<String, String> = HashMap()
        map["username"] = username
        map["smsCode"] = smsCode
        val s = GsonUtil.toJsonStr(map)
        val requestBody = RequestBody.create(parse.parse("application/json"), s)
        addTask(
            RetrofitHelper.getInstance().service.login(requestBody)
        ) { resp: BaseModel<LoginModel> ->
            val data = resp.data
            view!!.onLoginSucceed(data)
        }
    }

    /**
     * 获取验证码
     *
     * @param type
     * @param phone
     */
    override fun getSmsCode2(type: Int, phone: String) {
        val map: MutableMap<String, Any> = HashMap()
        map["type"] = type
        map["phone"] = phone
        val s = GsonUtil.toJsonStr(map)
        val requestBody = RequestBody.create(parse.parse("application/json"), s)
        addTask(
            RetrofitHelper.getInstance().service.getMsg(requestBody),
            Consumer<BaseModel<String?>> { resp: BaseModel<String?>? ->
                view.getCodeSuccess()
            })
    }
}
